<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<%
String p = request.getParameter("p");
if (p == null) {
	session.setAttribute("row", 0);
} else {
	int pages = 0;
	try {
		pages = Integer.parseInt(p);	
	} catch (NumberFormatException e) {
//		e.printStackTrace();
	}
	session.setAttribute("row", (pages)*10);
}
%>
<table align="center">
<caption><h1>查詢班表</h1></caption>
<form method="post" action="index.jsp">
<tr><td>員工編號：</td><td><input type="text" name="user_id" value="${member.user_id}" /></td></tr>
<tr><td>查詢開始日期：</td><td><input type="text" name="date_begin" class="datepicker" value="${param.date_begin }"/></td></tr>
<tr><td>查詢結束日期： </td><td><input type="text" name="date_end" class="datepicker" value="${param.date_end }"/></td></tr>
<input type="hidden" name="f" value="3" /> 
<tr><td></td><td><input type="submit" value="查詢" /><input type="button" value="重新查詢" onclick="self.location.href='index.jsp?f=3'"></td></tr>
</form>
</table>
<br><br>
<sql:query var="rs" dataSource="jdbc/j2db" maxRows="10" startRow="${row }">
	select *
	from shift, worktypes
	where user_id = ?
	and (shift_date between ? and ?)
	and shift.wt_number = worktypes.wt_number
	order by shift_date asc
	<sql:param>${param.user_id }</sql:param>
	<sql:param>${param.date_begin }</sql:param>
	<sql:param>${param.date_end }</sql:param>
</sql:query>
<sql:query var="rs2" dataSource="jdbc/j2db">
	select *
	from shift, worktypes
	where user_id = ?
	and (shift_date between ? and ?)
	and shift.wt_number = worktypes.wt_number
	order by shift_date asc
	<sql:param>${param.user_id }</sql:param>
	<sql:param>${param.date_begin }</sql:param>
	<sql:param>${param.date_end }</sql:param>
</sql:query>
<table align="center">
	<tr>
		<th>日期</th>
		<th>上班時間</th>
		<th>下班時間</th>
	</tr>
<c:forEach var="r" items="${rs.rows }">
	<tr>
		<td>${r.shift_date }</td>
		<td>${r.on_time }</td>
		<td>${r.off_time }</td>
	</tr>
</c:forEach>
</table>

<fmt:formatNumber var="pages" value="${row/10 }" pattern="#" />
<fmt:formatNumber var="allpages" value="${rs2.rowCount/10 }" pattern="#" />

第
<c:choose>
<c:when test="${allpages > 0}">
<c:out value="${pages+1 }" />&nbsp;
</c:when>
<c:otherwise>
<c:out value="${pages }" />&nbsp;
</c:otherwise>
</c:choose>
/
<c:out value="${allpages }" />
頁&nbsp;
<c:if test="${allpages > 1 }">
	<a href="index.jsp?f=3&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }">頁首</a>
</c:if>

<c:if test="${pages < allpages && row != 0}">
	<a href="index.jsp?f=3&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }&p=${pages-1 }">上一頁</a>
</c:if>

<c:if test="${pages+1 < allpages && row != null}">
	<a href="index.jsp?f=3&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }&p=${pages+1 }">下一頁</a>
</c:if>

<c:if test="${allpages > 1 }">
	<a href="index.jsp?f=3&user_id=${param.user_id }&date_begin=${param.date_begin }&date_end=${param.date_end }&p=${allpages-1}">頁尾</a>
</c:if>
